home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / c / simpleguide1.lha / simpleguide.c next >
C/C++ Source or Header  |  1995-02-06  |  12KB  |  510 lines

  1. /*
  2. **    Support code for easy implementation of the AmigaGuide help system
  3. **    in an application. These routines are fully reentrant and 
  4. **  self-contained.  All routines are safe to call even if AmigaGuide
  5. **  is not running.
  6. **
  7. **    Copyright (C) 1995 Petter Nilsen of Ultima Thule Software,
  8. **                        All Rights Reserved.
  9. **
  10. */
  11.  
  12. #include <libraries/amigaguide.h>
  13. #include <exec/memory.h>
  14. #include <dos/dos.h>
  15.  
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18. #include <proto/amigaguide.h>
  19.  
  20. struct GuideContext
  21. {
  22.     AMIGAGUIDECONTEXT     guidecontext;
  23.     ULONG                 guidesignal;
  24.     struct Library        *guidebase;
  25.     struct ExecBase        *SysBase;
  26. };
  27.  
  28. #define GUIDECONTEXT
  29. typedef struct GuideContext *GuideContext;
  30.  
  31. #include "simpleguide.h"
  32.  
  33.  
  34. /****** simpleguide/AmigaGuideOpen ******************************************
  35. *
  36. *   NAME    
  37. *       AmigaGuideOpen -- Open async AmigaGuide prosess ready for input
  38. *
  39. *   SYNOPSIS
  40. *       guide = AmigaGuideOpen(nag)
  41. *       D0                     A0
  42. *
  43. *       GuideContext AmigaGuideOpen(struct NewAmigaGuide *);
  44. *
  45. *   FUNCTION
  46. *       This function will start AmigaGuide async and wait until the help 
  47. *       system has been successfully started. 
  48. *       The amigaguide.library V34+ will also be opened.
  49. *
  50. *   INPUTS
  51. *       nag - a pointer to an initialized NewAmigaGuide structure. 
  52. *             Unused fields in this structure must be set to 0.
  53. *
  54. *   RESULT
  55. *       guide - A pointer to a GuideContext, or NULL on failure.
  56. *
  57. *   EXAMPLE
  58. *
  59. *       enum {
  60. *           HELP_MAIN,
  61. *           HELP_MACROKEYS,
  62. *       };
  63. *
  64. *       GuideContext guide = NULL;
  65. *
  66. *       struct NewAmigaGuide nag = {NULL};
  67. *
  68. *       STRPTR context[] = 
  69. *       {     
  70. *           "Main",
  71. *           "Macro_Keys",        [Note that AmigaGuide won't allow spaces]
  72. *           NULL                [in a node name!]
  73. *        };
  74. *
  75. *       nag.nag_BaseName = "THOR Help";
  76. *       nag.nag_Name = "THOR.guide";            [Set the document name]
  77. *       nag.nag_Screen = Scr;                   [screen pointer]
  78. *       nag.nag_Context = context;                [context table]
  79. *       nag.nag_Node = HELP_MAIN;                [node to align on first]
  80. *
  81. *       guide = AmigaGuideOpen(&nag);
  82. *
  83. *       [....]
  84. *
  85. *       [We need some help here]
  86. *
  87. *       if(guide)
  88. *       {
  89. *           [Set up what node we want help on]
  90. *
  91. *           SetGuideContext(guide, HELP_MACROKEYS);
  92. *
  93. *           [Start the show]
  94. *
  95. *           SendGuideContext(guide);
  96. *
  97. *           [Optional: Set the node pointer back to the first node]
  98. *    
  99. *           SetGuideContext(guide, HELP_MAIN);
  100. *       }
  101. *
  102. *
  103. *   NOTES
  104. *       The AmigaGuide help system must be closed with the AmigaGuideClose()
  105. *       function.
  106. *       Safe to call with a NULL pointer.
  107. *
  108. *   BUGS
  109. *
  110. *   SEE ALSO
  111. *       AmigaGuideClose, HandleAmigaGuide
  112. *
  113. ******************************************************************************
  114. *
  115. */
  116.  
  117. __asm GuideContext AmigaGuideOpen(register __a0 struct NewAmigaGuide *nag)
  118. {
  119.     struct Library *AmigaGuideBase;
  120.     struct AmigaGuideMsg *agm;
  121.     GuideContext        guide;
  122.     BOOL                OPEN;
  123.     BOOL                ACTIVE;
  124.     struct ExecBase *SysBase = *(struct ExecBase **)4L;
  125.     UBYTE ag_lib[20];
  126.  
  127.     /* We are really serious about having no global data.. :-) */
  128.  
  129.     ag_lib[0] = 'a';
  130.     ag_lib[1] = 'm';
  131.     ag_lib[2] = 'i';
  132.     ag_lib[3] = 'g';
  133.     ag_lib[4] = 'a';
  134.     ag_lib[5] = 'g';
  135.     ag_lib[6] = 'u';
  136.     ag_lib[7] = 'i';
  137.     ag_lib[8] = 'd';
  138.     ag_lib[9] = 'e';
  139.     ag_lib[10] = '.';
  140.     ag_lib[11] = 'l';
  141.     ag_lib[12] = 'i';
  142.     ag_lib[13] = 'b';
  143.     ag_lib[14] = 'r';
  144.     ag_lib[15] = 'a';
  145.     ag_lib[16] = 'r';
  146.     ag_lib[17] = 'y';
  147.     ag_lib[18] = '\0';
  148.  
  149.  
  150.     if(!nag) 
  151.         return(NULL);
  152.  
  153.     OPEN = FALSE;
  154.     ACTIVE = FALSE;
  155.  
  156.     if(guide = (GuideContext)AllocVec(sizeof(struct GuideContext), MEMF_CLEAR))
  157.     {
  158.         if(AmigaGuideBase = OpenLibrary(ag_lib, 34L))
  159.         {
  160.             guide->guidebase = AmigaGuideBase;
  161.             guide->SysBase = SysBase;
  162.  
  163.             if(guide->guidecontext = OpenAmigaGuideAsync(nag, NULL))
  164.             {
  165.                 if(guide->guidesignal = AmigaGuideSignal(guide->guidecontext))
  166.                 {
  167.                     OPEN = TRUE;
  168.  
  169.                     Wait(guide->guidesignal);
  170.  
  171.                     while(!ACTIVE)
  172.                     {
  173.                         while(agm = GetAmigaGuideMsg(guide->guidecontext))
  174.                         {
  175.                             /* Ok startup of the guide file */
  176.                             if(agm->agm_Type == ActiveToolID)
  177.                                 ACTIVE = TRUE;
  178.  
  179.                             /* Opening the guide file failed for some reason, continue as usual */
  180.                             if(agm->agm_Type == ToolStatusID && agm->agm_Pri_Ret)
  181.                                 ACTIVE = TRUE;
  182.  
  183.                             ReplyAmigaGuideMsg(agm);
  184.                         }
  185.                     }
  186.                 }
  187.             }
  188.         }
  189.         if(!OPEN)
  190.         {
  191.             if(guide) 
  192.             {
  193.                 AmigaGuideClose(guide);
  194.                 guide = NULL;
  195.             }
  196.         }
  197.     }
  198.     return(guide);
  199. }
  200.  
  201. /****** simpleguide/AmigaGuideClose ******************************************
  202. *
  203. *   NAME    
  204. *       AmigaGuideClose -- Close async AmigaGuide prosess already opened with
  205. *                          AmigaGuideOpen().
  206. *
  207. *   SYNOPSIS
  208. *       AmigaGuideClose(guide)
  209. *                       A0
  210. *
  211. *       void AmigaGuideClose(GuideContext);
  212. *
  213. *   FUNCTION
  214. *       This function will close the AmigaGuide async prosess already opened
  215. *       with AmigaGuideOpen() and free the structure passed in. If AmigaGuide
  216. *       have a window open, this window will be closed. This function should
  217. *       used if you close the screen or your application is finished running.
  218. *
  219. *   INPUTS
  220. *       guide - a pointer to an initialized GuideContext structure optained with
  221. *               a call to AmigaGuideOpen().
  222. *
  223. *   RESULT
  224. *
  225. *   EXAMPLE
  226. *
  227. *   NOTES
  228. *       Safe to call with a NULL pointer.
  229. *
  230. *   BUGS
  231. *
  232. *   SEE ALSO
  233. *       AmigaGuideOpen, HandleAmigaGuide
  234. *
  235. ******************************************************************************
  236. *
  237. */
  238.  __asm void AmigaGuideClose(register __a0 GuideContext guide)
  239. {
  240.     struct Library *AmigaGuideBase;
  241.     struct ExecBase *SysBase;
  242.  
  243.     if(guide)
  244.     {
  245.         AmigaGuideBase = guide->guidebase;
  246.         SysBase = guide->SysBase;
  247.  
  248.         if(guide->guidecontext)
  249.             CloseAmigaGuide(guide->guidecontext);
  250.  
  251.         if(guide->guidebase)
  252.             CloseLibrary(guide->guidebase);
  253.  
  254.         FreeVec(guide);
  255.     }
  256. }
  257.  
  258. /****** simpleguide/HandleAmigaGuide ******************************************
  259. *
  260. *   NAME    
  261. *       HandleAmigaGuide - will simple reply to all outstanding messages 
  262. *                          from the AmigaGuide prosess started with
  263. *                          AmigaGuideOpen().
  264. *
  265. *
  266. *   SYNOPSIS
  267. *       HandleAmigaGuide(guide)
  268. *                        A0
  269. *
  270. *       void HandleAmigaGuide(GuideContext);
  271. *
  272. *   FUNCTION
  273. *       This function will simple reply to all outstanding messages from the
  274. *       AmigaGuide prosess given with the GuideContext argument.
  275. *
  276. *   INPUTS
  277. *       guide - a pointer to an initialized GuideContext structure optained with
  278. *               a call to AmigaGuideOpen().
  279. *
  280. *   RESULT
  281. *
  282. *   EXAMPLE
  283. *
  284. *   NOTES
  285. *       Safe to call with a NULL pointer.
  286. *
  287. *   BUGS
  288. *
  289. *   SEE ALSO
  290. *       AmigaGuideOpen, AmigaGuideClose
  291. *
  292. ******************************************************************************
  293. *
  294. */
  295.  __asm void HandleAmigaGuide(register __a0 GuideContext guide)
  296. {
  297.     struct Library    *AmigaGuideBase;
  298.     struct AmigaGuideMsg *agm;
  299.  
  300.     if(guide)
  301.     {
  302.         AmigaGuideBase = guide->guidebase;
  303.  
  304.         while(agm = GetAmigaGuideMsg(guide->guidecontext))
  305.             ReplyAmigaGuideMsg(agm);
  306.     }
  307. }
  308.  
  309. /****** simpleguide.c/GetAmigaGuideSignal ******************************************
  310. *
  311. *   NAME    
  312. *       GetAmigaGuideSignal - will return a signal to be used in event-loops
  313. *
  314. *
  315. *   SYNOPSIS
  316. *       signal = GetAmigaGuideSignal(guide)
  317. *       D0                           A0
  318. *
  319. *       ULONG GetAmigaGuideSignal(GuideContext);
  320. *
  321. *   FUNCTION
  322. *       This function will simply return a signal to be used in event loops.
  323. *
  324. *   INPUTS
  325. *       guide - a pointer to an initialized GuideContext structure optained with
  326. *               a call to AmigaGuideOpen().
  327. *
  328. *   RESULT
  329. *       signal - A signal previously optained in the GuideContext structure
  330. *            after calling AmigaGuideOpen().  Will return 0 if the pointer
  331. *            passed in is NULL.
  332. *
  333. *   EXAMPLE
  334. *
  335. *   NOTES
  336. *       Safe to call with a NULL pointer, and will return 0L if this is the
  337. *       case. 
  338. *
  339. *   BUGS
  340. *
  341. *   SEE ALSO
  342. *       AmigaGuideOpen, AmigaGuideClose
  343. *
  344. ******************************************************************************
  345. *
  346. */
  347. __asm ULONG GetAmigaGuideSignal(register __a0 GuideContext guide)
  348. {
  349.     if(guide)
  350.         return(guide->guidesignal);
  351.     else
  352.         return(0L);
  353. }
  354.  
  355. /****** simpleguide/GetAmigaGuideBase ******************************************
  356. *
  357. *   NAME    
  358. *       GetAmigaGuideBase - will return a pointer to AmigaGuideBase
  359. *
  360. *   SYNOPSIS
  361. *       base = GetAmigaGuideBase(guide)
  362. *       D0                       A0
  363. *
  364. *       struct Library * GetAmigaGuideBase(GuideContext);
  365. *
  366. *   FUNCTION
  367. *       This function will return a pointer to AmigaGuideBase and must be called
  368. *       first if you intent to call any functions in amigaguide.library directly.
  369. *
  370. *   INPUTS
  371. *       guide - a pointer to an initialized GuideContext structure optained with
  372. *               a call to AmigaGuideOpen().
  373. *
  374. *   RESULT
  375. *       base - A AmigaGuideBase pointer previously optained in the GuideContext 
  376. *          structure after calling AmigaGuideOpen().  Will return NULL if the 
  377. *          pointer passed in is NULL.
  378. *
  379. *   EXAMPLE
  380. *
  381. *       struct Library *AmigaGuideBase;
  382. *
  383. *       if(AmigaGuideBase = GetAmigaGuideBase(guidecontext))
  384. *       {
  385. *           [safely call amigaguide.library functions directly here]
  386. *       }
  387. *
  388. *   NOTES
  389. *       Safe to call with a NULL pointer, and will return NULL if this is the
  390. *       case. 
  391. *
  392. *   BUGS
  393. *
  394. *   SEE ALSO
  395. *       AmigaGuideOpen, AmigaGuideClose
  396. *
  397. ******************************************************************************
  398. *
  399. */
  400. __asm struct Library *GetAmigaGuideBase(register __a0 GuideContext guide)
  401. {
  402.     if(guide)
  403.         return(guide->guidebase);
  404.     else
  405.         return(NULL);
  406. }
  407.  
  408. /****** simpleguide/SetGuideContext ******************************************
  409. *
  410. *   NAME    
  411. *       SetGuideContext -- Set the context ID for an AmigaGuide system.
  412. *
  413. *   SYNOPSIS
  414. *       success = SetGuideContext(guide, node)
  415. *       D0                        A0     D0
  416. *
  417. *       BOOL SetGuideContext(GuideContext, ULONG);
  418. *
  419. *    FUNCTION
  420. *       This function, and the SendGuideContext() function, are used to
  421. *       provide a simple way to display a node based on a numeric value,
  422. *       instead of having to build up a slightly more complex command
  423. *       string.
  424. *
  425. *   INPUTS
  426. *       guide - a pointer to an initialized GuideContext structure optained with
  427. *          a call to AmigaGuideOpen().
  428. *
  429. *       node  - Index value of the desired node to display.
  430. *
  431. *   RESULT
  432. *       success - Returns TRUE if a valid context ID and GuideContext pointer was 
  433. *          passed, otherwise returns FALSE.
  434. *
  435. *   EXAMPLE
  436. *
  437. *   NOTES
  438. *       Safe to call with a NULL pointer, and will return FALSE if this is the
  439. *       case. 
  440. *
  441. *   BUGS
  442. *
  443. *   SEE ALSO
  444. *       SetAmigaGuideContext, SendGuideContext
  445. *
  446. ******************************************************************************
  447. *
  448. */
  449. __asm BOOL SetGuideContext(register __a0 GuideContext guide, register __d0 ULONG index)
  450. {
  451.     struct Library    *AmigaGuideBase;
  452.  
  453.     if(guide)
  454.     {
  455.         AmigaGuideBase = guide->guidebase;
  456.         return((BOOL)SetAmigaGuideContext(guide->guidecontext, index, NULL));
  457.     }
  458.     return(FALSE);
  459. }
  460.  
  461. /****** simpleguide/SendGuideContext ******************************************
  462. *
  463. *   NAME    
  464. *       SendGuideContext - Align an AmigaGuide system on the context ID.
  465. *
  466. *   SYNOPSIS
  467. *       success = SendGuideContext(guide)
  468. *       D0                         A0
  469. *
  470. *       BOOL SendGuideContext(GuideContext);
  471. *
  472. *    FUNCTION
  473. *       This function is used to send a message to an AmigaGuide system to
  474. *       align it on the current context ID.
  475. *
  476. *   INPUTS
  477. *       guide - a pointer to an initialized GuideContext structure optained with
  478. *          a call to AmigaGuideOpen().
  479. *
  480. *   RESULT
  481. *       success - Returns TRUE if a valid GuideContext pointer was passed and the
  482. *          message was sent, otherwise returns FALSE.
  483. *
  484. *   EXAMPLE
  485. *
  486. *   NOTES
  487. *       Safe to call with a NULL pointer, and will return FALSE if this is the
  488. *       case. 
  489. *
  490. *   BUGS
  491. *
  492. *   SEE ALSO
  493. *       SendAmigaGuideContext, SetGuideContext
  494. *
  495. ******************************************************************************
  496. *
  497. */
  498. __asm BOOL SendGuideContext(register __a0 GuideContext guide)
  499. {
  500.     struct Library    *AmigaGuideBase;
  501.  
  502.     if(guide)
  503.     {
  504.         AmigaGuideBase = guide->guidebase;
  505.         return((BOOL)SendAmigaGuideContext(guide->guidecontext, NULL));
  506.     }
  507.     return(FALSE);
  508. }
  509.  
  510.